<template>
  <div>
    <!-- 顶部卡片 -->
    <el-card class="top" shadow="always">
      <div class="content">
        <!-- 左侧 -->
        <div class="left" @click="goHome">
          <img src="../../assets/images/logo.png" alt="logo" />
          <p>尚医通&nbsp;&nbsp;预约挂号统一平台</p>
        </div>
        <!-- 右侧 -->
        <div class="right">
          <!-- 切换深色模式的开关 -->
          <div class="dark">
            <el-switch
              v-model="dark"
              inline-prompt
              style="--el-switch-on-color: #333; --el-switch-off-color: #ddd"
              active-text="Dark"
              active-action-icon="Moon"
              inactive-text="Light"
              inactive-action-icon="Sunny"
              size="large"
              @change="changeDark"
            ></el-switch>
          </div>
          <!-- 如果用户仓库/本地存储中没有用户数据展示登录注册 -->
          <p v-if="!userName" class="login" @click="showLogin = true">
            登录/注册
          </p>
          <!-- 如果用户仓库/本地存储中有用户数据展示下拉菜单 -->
          <el-dropdown v-else>
            <span class="el-dropdown-link">
              <p>{{ userName }}</p>
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goIdentification">
                  实名认证
                </el-dropdown-item>
                <el-dropdown-item @click="goOrder">挂号订单</el-dropdown-item>
                <el-dropdown-item @click="goPatient">
                  就诊人管理
                </el-dropdown-item>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts" name="HospitalTop">
import useThemeStore from '@/store/modules/theme'
import useUserStore from '@/store/modules/user'
import { SET_THEME } from '@/utils/theme'
import { ElMessage } from 'element-plus'
import { storeToRefs } from 'pinia'
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'

// 获取用户仓库中的数据控制登录对话框的显示与隐藏
const { showLogin, userName } = storeToRefs(useUserStore())
const { userLogOut } = useUserStore()
// 创建路由器对象
const router = useRouter()
// 从仓库中获取控制是否为深色模式的属性
const { dark } = storeToRefs(useThemeStore())

// 点击LOGO或左侧文字路由跳转到首页
const goHome = () => {
  router.push('/home')
}
// 点击切换深色模式的按钮的回调
const changeDark = () => {
  // 获取HTML根节点 为根节点添加一个类名class="dark"
  let html = document.documentElement
  // 根据dark的值为html根节点添加或者删除dark类名
  dark.value ? (html.className = 'dark') : (html.className = '')
  // 持久化存储主题值(已用piniaPluginPersistedstate插件)
  // SET_THEME(dark.value)
}
// 点击跳转实名认证的按钮回调
const goIdentification = () => {
  router.push({
    name: 'identification',
  })
}
// 点击跳转挂号订单的按钮回调
const goOrder = () => {
  router.push({
    name: 'order',
  })
}
// 点击跳转就诊人管理的按钮回调
const goPatient = () => {
  router.push({
    name: 'patient',
  })
}
// 点击退出登录按钮的回调
const logout = () => {
  // 通知仓库退出登录
  userLogOut()
  // 路由跳转到首页
  router.replace('/home')
  // 退出成功提示
  ElMessage.success('退出登录成功')
}
onMounted(() => {
  // 初始化主题颜色
  changeDark()
})
</script>

<style scoped lang="scss">
.top {
  position: fixed;
  z-index: 999;
  width: 100%;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  .content {
    width: 1200px;
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      img {
        width: 50px;
        height: 50px;
        margin-right: 10px;
      }
      p {
        color: $base-active-color;
        font-size: 20px;
        // font-weight: bold;
      }
    }
    .right {
      cursor: pointer;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 13px;
      color: $base-active-color;
      .dark {
        margin-right: 10px;
      }
    }
  }
}
.el-dropdown-link {
  color: $base-active-color;
  cursor: pointer;
  display: flex;
  align-items: center;
  margin-top: -1px;
}
</style>
